//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: list_factory.c 757 2007-05-10 08:19:31Z roger $
//

#include <stdio.h>
#include <glib.h>
#include <string.h>
#include <gtk/gtk.h>

#include "prefedit.h"
#include "list_factory.h"
#include "Prefedit_cb.h"

static GList *Symbol_retrieve_list(const gchar *string);
static GList *Word_retrieve_list(const gchar *string);

void
IM_free_list(GList *list)
{
	GList *l = list;

	while (l) {
		IMInfo *info = (IMInfo *)l->data;
		g_free(info->id);
		g_free(info->cname);
		l = g_list_next(l);
	}
	g_list_free(list);
}

GList *
IM_get_curr_list(void)
{
	GtkSelectionData *seldata;
	int num_im;
	GList *l;

	seldata = gtk_clipboard_wait_for_contents(clip_status,
			gdk_atom_intern("_IM_CURR_LIST", FALSE));

	if (!seldata)
		return NULL;

	num_im = atoi((char *)seldata->data);
	if (num_im < 1)
		return NULL;

	l = IM_retrieve_list((gchar *)seldata->data);
	gtk_selection_data_free(seldata);
	return l;
}

void
IM_shift_up(GList *trunk, GList *node)
{
	GList *prev = node->prev;
	GList *next = node->next;

	if (!node || !node->prev)
		return;

	node->prev = prev->prev;
	node->next = prev;

	prev->prev = node;
	prev->next = next;
}

void
IM_shift_down(GList *trunk, GList *node)
{
	GList *prev = node->prev;
	GList *next = node->next;

	if (!node || !node->next)
		return;

	node->prev = next;
	node->next = next->next;

	next->prev = prev;
	next->next = node;
}

GtkListStore *
list_store_new_from_im_list(GList *list)
{
        GtkListStore *store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
        
        GList *l = list;
        while (l) {
                IMInfo *info = (IMInfo *)l->data;
                GtkTreeIter iter;
                gtk_list_store_append(store, &iter);
                gtk_list_store_set(store, &iter, STORE_1, info->cname, STORE_2, info->id, -1);
                l = g_list_next(l);
        }

        return store;
}


GList *
IM_get_all_list(void)
{
	GtkSelectionData *seldata;
	int num_im;
	GList *l;

	seldata = gtk_clipboard_wait_for_contents(clip_status,
			gdk_atom_intern("_IM_ALL_LIST", FALSE));

	if (!seldata)
		return NULL;

	num_im = atoi((char *)seldata->data);
	if (num_im < 1)
		return NULL;

	l = IM_retrieve_list((gchar *)seldata->data);
	gtk_selection_data_free(seldata);
	return l;
}

GList *
IM_retrieve_list(const gchar *im_list_str)
{
	gchar **im_list_strv = NULL;
	int num_im, i;
	GList *im_list = NULL;

	im_list_strv = g_strsplit(im_list_str, "\n", 0);
	num_im = atoi(im_list_strv[0]);
	for (i=0; i<num_im; i++) {
		IMInfo *info = g_new0(IMInfo, 1);

		info->id = g_strdup(im_list_strv[i*2 + 1]);
		info->cname = g_strdup(im_list_strv[i*2 + 2]);
		im_list = g_list_append(im_list, info);

	}
	g_strfreev(im_list_strv);

	return im_list;
}


void
Symbol_free_list(GList *list)
{
	GList *l = list;

	if (!list)
		return;

	while (l) {
		SymDefGroup *pSDG;
		pSDG = (SymDefGroup *)l->data;
		g_free(pSDG->symbol);

		l = g_list_next(l);
	}
	g_list_free(list);
}

GList *
Symbol_get_list(void)
{
	GtkSelectionData *seldata;
	int num;
	GList *l = NULL;

	seldata = gtk_clipboard_wait_for_contents(clip_status,
			gdk_atom_intern("_SYMBOL_LIST", FALSE));
	if (!seldata)
		return NULL;

	num = atoi((gchar *)seldata->data);
	if (num < 1)
		return NULL;

	l = Symbol_retrieve_list((gchar *)seldata->data);
	gtk_selection_data_free(seldata);
	return l;
}

static GList *
Symbol_retrieve_list(const gchar *string)
{
	gchar **list_str = NULL;
	int num, i;
	GList *l = NULL;

	list_str = g_strsplit(string, "\n", 0);
	num = atoi(list_str[0]);
	for (i=0; i<num; i++) {
		SymDefGroup *pSDG = g_new0(SymDefGroup, 1);

		pSDG->modifier = atoi(list_str[i*3 + 1]);
		pSDG->key = atoi(list_str[i*3 + 2]);
		pSDG->symbol = g_strdup(list_str[i*3 + 3]);
		l = g_list_append(l, pSDG);
	}
	g_strfreev(list_str);
	return l;
}

void
Word_free_list(GList *list)
{
	GList *l = NULL;
	WordDefGroup *pWDG = NULL;

	while (l) {
		pWDG = (WordDefGroup *)l->data;
		g_free(pWDG->key);
		g_free(pWDG->word);
		l = g_list_next(l);
	}
	g_list_free(list);
}

GList *Word_get_list(void)
{
	GtkSelectionData *seldata;
	int num;
	GList *l = NULL;

	seldata = gtk_clipboard_wait_for_contents(clip_status,
			gdk_atom_intern("_WORD_LIST", FALSE));
	if (!seldata)
		return NULL;

	num = atoi((gchar *)seldata->data);
	if (num < 1)
		return NULL;

	l = Word_retrieve_list((gchar *)seldata->data);
	gtk_selection_data_free(seldata);
	return l;
}

static GList *
Word_retrieve_list(const gchar *string)
{
	gchar **list_str = NULL;
	int num, i;
	GList *l = NULL;

	list_str = g_strsplit(string, "\n", 0);
	num = atoi(list_str[0]);
	for (i=0; i<num; i++) {
		WordDefGroup *pWDG = g_new0(WordDefGroup, 1);

		pWDG->key = g_strdup(list_str[i*2 + 1]);
		pWDG->word = g_strdup(list_str[i*2 + 2]);
		l = g_list_append(l, pWDG);
	}
	g_strfreev(list_str);
	return l;
}

// vi:ts=4:nowrap:cin:expandtab
